package cn.devices.mapper;

import cn.devices.dto.InventoryDto;
import cn.devices.entity.TbInventory;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * 盘点清单 Mapper 接口
 * </p>
 *
 * @author 秋水浮萍
 * @since 2021-02-07
 */
@Repository
public interface TbInventoryMapper {

    @Insert("insert into tb_inventory(task_id, device_code, code_time, device_status, device_spec,inventory_info,priority,device_img) " +
            "VALUE (#{taskId},#{deviceCode},#{codeTime},#{deviceStatus},#{deviceSpec},#{inventoryInfo},#{priority},#{deviceImg})")
    int tbInventoryAdd(TbInventory tbInventory);

    @Select("select device_status from tb_device where device_code=#{deviceCode}")
    String status(String deviceCode);

    @Insert("<script>" +
            "insert into tb_inventory(task_id, device_code, " +
            "inventory_info,priority) " +
            "values " +
            "<foreach collection='devices' item='devices' index='index' separator=','>" +
            "(#{taskId},#{devices},'未扫描到',1 )" +
            "</foreach>" +
            "</script>")
    int tbInventoryAdds(@Param("devices") List<String> devices, String taskId);

    @Select("select *\n" +
            "from tb_task\n" +
            "          join tb_inventory ti on tb_task.task_id = ti.task_id\n" +
            "        where ti.task_id=#{taskId} order by device_status desc")
    List<TbInventory> testInventoryList(String taskId);

    @Select("select * from tb_inventory where task_id=#{taskId} and device_code=#{deviceCode}")
    TbInventory tbInventoryById(String taskId, String deviceCode);

    @Select("select tb_inventory.*,\n" +
            "       td.device_img\n" +
            "from tb_inventory\n" +
            "         left join tb_device td on tb_inventory.device_code = td.device_code\n" +
            "where task_id = #{taskId}\n" +
            "order by priority")
    List<InventoryDto> inventoryDt(String taskId);

}
